package leetcode.datastructure.array;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Program88MergeSortedArray {

    public Program88MergeSortedArray() {
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        if (n == 0) {
            return;
        }
        if (m == 0) {
            for (int t = 0; t < n; t++) {
                nums1[t] = nums2[t];
            }
        }
        int index = m + n - 1;
        int i = m - 1, j = n - 1;
        for (; (i >= 0) && (j >= 0);) {
            if (nums1[i] < nums2[j]) {
                nums1[index] = nums2[j];
                j--;
            } else {
                nums1[index] = nums1[i];
                i--;
            }
            index--;
        }
        while (j >= 0) {
            nums1[index] = nums2[j];
            j--;
            index--;
        }

        System.out.println(Arrays.toString(nums1));
    }

    public static void main(String[] args) {
        Program88MergeSortedArray p = new Program88MergeSortedArray();
        int[] nums1 = new int[]{4,5,6,0,0,0};
        int[] nums2 = new int[]{1,2,3};
        p.merge(nums1, 3, nums2,3);
    }
}
